<template>
  <div>
    <div class="aside">
      <h4 class="aside-title">相关攻略</h4>
      <div
        class="recommend-list"
        v-for="item in postDetailsRightData"
        :key="item.id"
      >
        <!-- 所以如果在想要在router-link上添加事件的话需要@click.native这样写 -->
        <nuxt-link
          :to="`/post/postDetails?id=${item.id}`"
          class="recommend-item"
          @click.native="getPost"
        >
          <el-row
            type="flex"
            class="post-imgText"
            v-if="item.images.length > 0"
          >
            <el-row typt="flex" class="post-img" align="middle">
              <img :src="item.images[0]" alt="" />
            </el-row>

            <div class="post-text">
              <div>{{ item.title }}</div>
              <p>
                {{ item.updated_at | moment }}
                阅读
                {{ item.watch }}
              </p>
            </div>
          </el-row>
          <el-row type="flex" class="post-imgText" v-else>
            <div class="post-text">
              <div>{{ item.title }}</div>
              <p>
                {{ item.updated_at | moment }}
                阅读
                {{ item.watch }}
              </p>
            </div>
          </el-row>
        </nuxt-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      postDetailsRightData: {}
    };
  },
  created() {
    this.getRecommend();
  },
  methods: {
    getRecommend() {
      this.$axios({
        url: "/posts/recommend",
        params: this.$route.query
      }).then(res => {
        // console.log(res.data);
        this.postDetailsRightData = { ...res.data.data };
        // console.log(this.postDetailsRightData);
      });
    },
    getPost() {
      this.$emit("getPost", this.$route.query);
    }
  }
};
</script>

<style lang="less" scoped>
.aside {
  width: 280px;
  .aside-title {
    font-weight: 400;
    font-size: 18px;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
  }

  .recommend-item {
    display: block;
    padding: 20px 0;
    border-bottom: 1px solid #ddd;

    .post-imgText {
      height: 80px;

      .post-img {
        width: 100px;
        height: 80px;
        flex-shrink: 0;
        background: #ddd;
        overflow: hidden;
        margin-right: 10px;

        img {
          width: 100%;
          height: 100%;
          -o-object-fit: cover;
          object-fit: cover;
        }
      }

      .post-text {
        flex: 1;
        position: relative;

        div {
          position: relative;
          line-height: 1.4em;
          height: 2.8em;
          overflow: hidden;
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
        }

        p {
          position: absolute;
          bottom: 0;
          left: 0;
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}
</style>
